<template>
	<view class="container"><diy-component :diyItems="items"></diy-component></view>
</template>

<script>
import diyComponent from '@/components/diy/index.vue';
import { storageCache } from '@/store/dispatches.js';
import { mapState } from 'vuex';
export default {
	components: {
		diyComponent
	},
	data() {
		return {
			// 页面参数
			options: {},
			// 页面元素
			page: { style: {} },
			scrollTop: 0
		};
	},
	computed: {
		...mapState({
			items: state => state.cache.mallHomeData || {}
		})
	},
	onPullDownRefresh() {
		this.getPageData(() => {
			uni.stopPullDownRefresh();
		});
	},
	onLoad(options) {
		this.options = options;
		this.getPageData();
	},
	onShow() {
		setTimeout(() => {
			if (this.page.hasOwnProperty('params')) {
				this.setPageBar(this.page);
			}
		}, 0);
	},
	methods: {
		getPageData(callback) {
			this.$u.api
				.getPageInfo()
				.then(res => {
					// 设置顶部导航栏栏
					this.setPageBar(res.data.page);
					// _this.setData(result.data);
					storageCache({ key: 'mallHomeData', value: res.data.items });
					this.page = res.data.page;
					// 回调函数
					typeof callback === 'function' && callback();
					this.$nextTick(() => {
						// #ifdef H5
						// this.$wechat.initJssdk(this.setShare());
						// #endif
					});
				})
				.catch(e => {
					uni.stopPullDownRefresh();
				});
		},
		/**
		 * 设置顶部导航栏
		 */
		setPageBar(page) {
			// 设置页面标题
			uni.setNavigationBarTitle({
				title: page.params.title
			});
			// 设置navbar标题、颜色
			uni.setNavigationBarColor({
				frontColor: page.style.titleTextColor === 'white' ? '#ffffff' : '#000000',
				backgroundColor: page.style.titleBackgroundColor
			});
			uni.stopPullDownRefresh();
		}
	}
};
</script>

<style lang="scss">
.container {
	background: #fff;
}
</style>
